
%  circuits.mat contains microcircuitry definitions in cell arrays -- load it
% then make your changes, then run this script

%
% circuit:
% this cell array may be edited in the MATLAB variable editor just
% like a spreadsheet, or new columns and rows may be inserted via scripted
% commands
%
% circuit is a 2xNareas cell array.  
%   row 1 contains a cell array of N x 2 
%           col 1 - type string
%           col 2 - layer string
%   row 2 contains a double array of N x Ntypes+7 describing percent presynaptic connections
%           column order should be: extra_microciruitry_columns1, Types,
%           extra_microciuitry_columns_2
%
% example loading from old text file format in case you'd rather do that
% instead of editing the current circuits array

% fp = fopen('micro-m1.txt');
% c=textscan(fp,'%s%s%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f','CollectOutput',1);
% fclose(fp);
% fp = fopen('micro-s1.txt');
% d=textscan(fp,'%s%s%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f','CollectOutput',1);
% fclose(fp);
% circuit{1,1}=c{1};
% circuit{1,2}=d{1};
% circuit{2,1}=c{2};
% circuit{2,2}=d{2};
%% Types = {   <--- note this is no longer true... Types now contains much
%% more data!@#$
%     'nb1' 
%     'p23' 
%     'b23' 
%     'nb23' 
%     'ss4(L4)' 
%     'ss4(L23)' 
%     'p4' 
%     'b4' 
%     'nb4' 
%     'p5(L23)' 
%     'p5(L56)' 
%     'b5' 
%     'nb5' 
%     'p6(L4)' 
%     'p6(L54)' 
%     'b6' 
%     'nb6' 
%     'TCs' 
%     'TCn' 
%     'TIs' 
%     'TIn' 
%     'TRN' 
%     'STR(GPi)' 
%     'STR(GPe)' 
%     'fsSTR' 
%     'spnGPe' 
%     'fsGPe' 
%     'pSTN' 
%     'fsSTN' 
%     'spnGPi' 
%     'fsGPi' };
% 
% Layers = {
%     'L6'
%     'L5'
%     'L4'
%     'L23'
%     'L1'
%     'Th'
%     'RTN'
%     'STR'
%     'GPe'
%     'STN'
%     'GPi'
%     'SNc' };
% 
% Areas = {
%     'M1' #Neurons lower_left_x_mm lower_left_y_mm width_mm projection_list(e.g. {'M1','V1'})
%     'S1' ... };
% 
% 
% extra_microcircuitry_columns_1 = {
%     'type'
%     'layer'
%     'type%'
%     '#syn' };
% 
% extra_microcircuitry_columns_2 = { 
%     'WM/DTI' 
%     'BRNSTEM' 
%     'Sensory' };
%   

 


for x = 1:length(Circuit)    
    %Error checking for Areas
    for y = 2:size(Circuit{1,x},1)
        if( sum(ismember(Areas(:,1),Circuit{1,x}{y,5})) ~= 1 && ~strcmp(Circuit{1,x}{y,5},'-') )
            error(['Mismatch between Circuit and Areas @ cell ' num2str(x) ': ' Circuit{1,x}{y,5}]);
        end
    end
    
    %Error checking for Types
    for y = 2:size(Circuit{1,x},1)
        if( ~isempty(Circuit{1,x}{y,1}) )
            if( sum(ismember(Types(:,1),Circuit{1,x}{y,1})) ~= 1 )
                error(['Mismatch between Circuit and Types @ line ' num2str(y) ': ' Circuit{1,x}{y,1}]);
            end
        end
        
        if( sum(ismember(Types(:,1),Circuit{1,x}{y,6})) ~= 1 )
            error(['Mismatch between Circuit and Types @ line ' num2str(y) ': ' Circuit{1,x}{y,6}]);
        end
    end
    
    %Error checking for Layers
    for y = 2:size(Circuit{1,x},1)
        if( ~isempty(Circuit{1,x}{y,3}) )
            if( sum(ismember(Layers,Circuit{1,x}{y,3})) ~= 1 )
                error(['Mismatch between Circuit and Layers @ line ' num2str(y) ': ' Circuit{1,x}{y,3}]);
            end
        end
    end
    
end


% save any changes you've made!
save circuits.mat Circuit Types Layers Areas


fp=fopen('microcircuitry.dat','w');
for areai=1:size(Areas,1),
    % Header for area
    fprintf(fp,'%s\n',Areas{areai,1});

    temptable=Circuit{areai};
    fprintf(fp,'%% %-14s',temptable{1,1});
    fprintf(fp,' %-13s',temptable{1,2:end});
    fprintf(fp,'\n');
    
    % table
    for linei=2:size( temptable,1),
        if ~isempty(temptable{linei,1})
            fprintf(fp,'%-16s %-13f', temptable{linei,1:2});
        else
            % if empty, then write 'ditto' as column placeholders.
            fprintf(fp,'%-16s %-13s', '"','"');
        end
        if ~isempty(temptable{linei,3})
            fprintf(fp,' %-13s %-13d ', temptable{linei,3:4});
        else
            % if empty, then write 'ditto' as column placeholders.
            fprintf(fp,' %-13s %-13s ', '"','"');
        end
        fprintf(fp,'%-13s %-13s', temptable{linei,5:6});
        fprintf(fp,' %-13f', temptable{linei,7:15});
        fprintf(fp,' %-13d', temptable{linei,16:17});%learning rate times int
        fprintf(fp,' %-13d', temptable{linei,18:19});%DA times int
        fprintf(fp,' %-13f', temptable{linei,20:23});        
        fprintf(fp,' %-13d', temptable{linei,24});
        fprintf(fp,' %-13f', temptable{linei,25:26});
        fprintf(fp,'\n');
    end
    
    % Separator.
    if areai<size(Areas,1)
        fprintf(fp,'*\n');
    end
end
fclose(fp);        
        
fp=fopen('neuronspecs.dat','w');
fprintf(fp,'%-20s',Types{1,:});
fprintf(fp,'\n');
for coli=2:size(Types,1),
    fprintf(fp,' %-20s %-20d %-20d %-20d', Types{coli,1:4});
    fprintf(fp,' %-20f',Types{coli,5:27});
    fprintf(fp,' %-20d',Types{coli,28:29});
    fprintf(fp,' %-20f',Types{coli,30:end});
    fprintf(fp,'\n');
end
fclose(fp);

% Create the 3dgrid_input file from Area cell array
fp=fopen('areas.dat','w');
%fprintf(fp,'%d\n',size(Areas,1));
for i=1:size(Areas,1),
    fprintf(fp,'%s\n', Areas{i,1});
    fprintf(fp,'%d\n', Areas{i,2});
    fprintf(fp,'%f %f\n', Areas{i,3}, Areas{i,4});
    fprintf(fp,'%f\n', Areas{i,5});
    
end
fclose(fp);

% a note about errors in model building
%
% its always a good idea to ensure that dimensions of the various elements
% of Circuit match as they should.
%
% it is also a good idea to check that all the presynaptic percentages add
% up to 100:
% sum( Circuit{2,xx}(:,3:end), 2)
% where xx goes from 1 to length(Areas)
